﻿using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.ProjectVersion;
using EkStudio.com.bean.xml.reflection;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.DataSource;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.RootInterface.language;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.com.bean.xml.RootInterface.QuerySource;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.common.unit;
using System.Collections.Generic;
using System;
using EkStudio.com.bean.xml.RootInterface.FieldEvent;

namespace EkStudio.com.bean.xml.BillChart
{
    [XmlRootElement("root")]
    public class BillChartModuleItem : BillFormatModule
    {
        //private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillChartModuleItem));

        private bool readOnly = false;

        private BillChartModuleInfoRoot chartProperties = null;//统计图bean配置属性

        private BillQuerySourceRoot querySource = null;//查询界面方案

        private BillChartModuleFieldsRoot fieldProperties = null;//查询字段属性

        private BillExpandCodeRoot expandCode = null;//扩展代码

        private BillFieldsI18nRoot i18nProperties = null;//多语言集合

        [XmlElement]
        public BillChartModuleInfoRoot ChartProperties
        {
            get { return chartProperties; }

            set { chartProperties = value; }
        }
        [XmlElement]
        public BillQuerySourceRoot QuerySource
        {
            get { return querySource; }

            set { querySource = value; }
        }
        [XmlElement]
        public BillChartModuleFieldsRoot FieldProperties
        {
            get { return fieldProperties; }
            set { fieldProperties = value; }
        }
        [XmlElement]
        public BillExpandCodeRoot ExpandCode
        {
            get { return expandCode; }
            set { expandCode = value; }
        }

        [XmlElement]
        public BillFieldsI18nRoot I18nProperties
        {
            get { return i18nProperties; }
            set { i18nProperties = value; }
        }



        [XmlTransient]
        public BillSourceRoot DataSource
        {
            get { return null; }

            set { }
        }

        [XmlTransient]
        public object ParentBean
        {
            get { return null; }

            set { }
        }
        [XmlTransient]
        public BillUiGroupRoot UiField
        {
            get { return null; }

            set { }
        }
        [XmlTransient]
        public BillDataFillRoot DataFill
        {
            get { return null; }

            set { }
        }

        #region 接口方法

        public object Clone()
        {
            return null;
        }

        public int getBillType()
        {
            return ProjectUtil.TYPE_BILL_CHARTBAR;
        }

        public string getCaption()
        {
            return chartProperties.Caption;
        }

        public string getJavaPackage()
        {
            return chartProperties.JavaPackage;
        }

        public string getKey()
        {
            return chartProperties.Key;
        }

        public string getLanguageText()
        {
            return chartProperties.LanguageTexts;
        }

        public string getVersion()
        {
            return chartProperties.RootVersion;
        }

        public bool isReadOnly()
        {
            return readOnly;
        }

        public void setReadOnly(bool read)
        {
            readOnly = read;
        }

        #endregion

        /// <summary>
        /// 初始化多语言
        /// </summary>
        /// <param name="version"></param>
        public void autoInitLanguage(ProjectVersionItem version)
        {
            if (i18nProperties == null)
            {
                i18nProperties = new BillFieldsI18nRoot();
            }
            if (i18nProperties.Group == null)
            {
                i18nProperties.Group = new List<BillFieldsI18nGroup>();
            }
            List<BillFieldsI18nGroup> groups = i18nProperties.Group;
            if (version != null && version.Languages != null && version.Languages.Item != null)
            {
                List<ProjectLanguageElement> languages = version.Languages.Item;
                for (int i = 0; i < languages.Count; i++)
                {
                    string key = languages[i].Name;
                    string default_ = languages[i].Default_;
                    BillFieldsI18nGroup group = null;
                    for (int k = 0; k < groups.Count; k++)
                    {
                        if (groups[k].Key == key)
                        {
                            group = groups[k];
                            break;
                        }
                    }
                    if (group == null)
                    {
                        group = new BillFieldsI18nGroup();
                        group.Key = key;
                        group.Element = new List<BillFieldsI18nElement>();
                        groups.Add(group);
                    }
                    if (group.Element == null) group.Element = new List<BillFieldsI18nElement>();
                    this.createLanElement(group, (default_ == "Y"));
                }

            }

        }



        public void autoInitQueryCode()
        {
            //生成查询界面
            if (!readOnly)
            {//只有在非只读的情况下执行
                BillQuerySourceRoot root = QuerySource;
                if (root == null)
                {
                    root = new BillQuerySourceRoot();
                    QuerySource = root;
                }
                List<BillQuerySchemeItem> chemeList = root.Item;
                if (chemeList == null)
                {
                    chemeList = new List<BillQuerySchemeItem>();
                    root.Item = chemeList;
                }
                chemeList.Clear();
                BillQuerySchemeItem scheme = new BillQuerySchemeItem();
                scheme.Key = getKey();// setKey(this.getKey());
                scheme.Caption = getCaption();//setCaption(this.getCaption());
                scheme.Default_ = true;//setDefault_(true);
                scheme.LanguageTexts = getLanguageText();////setLanguageTexts(this.getLanguageTexts());
                chemeList.Add(scheme);

                if (this.fieldProperties != null && this.fieldProperties.Fields != null && this.fieldProperties.Fields.Count > 0)
                {

                    BillQueryPrinterFields printFields = new BillQueryPrinterFields();printFields.Fields = new List<BillQueryPrinterItem>();
                    BillQueryGeneralFields generalFields = new BillQueryGeneralFields();generalFields.Fields = new List<BillQueryPrinterItem>();
                    BillQueryAdvancedFields advancedFields = new BillQueryAdvancedFields();advancedFields.Fields = new List<BillQueryPrinterItem>();
                    BillQuerySqlSource sqlSource = new BillQuerySqlSource();
                    BillQueryWhereCode sqlWhere = new BillQueryWhereCode();

                    scheme.PrinterFields = printFields;//setPrinterFields(printFields);
                    scheme.GeneralFields = generalFields;//setGeneralFields(generalFields);
                    scheme.AdvancedFields = advancedFields;//setAdvancedFields(advancedFields);
                    scheme.SqlSource = sqlSource;//setSqlSource(sqlSource);
                    scheme.SqlWhere = sqlWhere;//setSqlWhere(sqlWhere);
                    //sql语句
                    sqlSource.Content = ChartProperties.Advanced.Content;//ChartProperties.getAdvanced().getContent();

                    for (int i = 0; i < fieldProperties.Fields.Count; i++)
                    {
                        BillUiFieldsItem field = this.fieldProperties.Fields[i];
                        if (field.EditProperty.Enable && field.DataProperty.DataSource != "" &&
                                (field.DataProperty.EditType == BeanUtil.KT_TEXTFIELD_TYPE
                                || field.DataProperty.EditType == BeanUtil.KT_MEMOFIELD_TYPE
                                || field.DataProperty.EditType == BeanUtil.KT_COMBOBOX_TYPE
                                || field.DataProperty.EditType == BeanUtil.KT_COMBOBOXGRID_TYPE
                                || field.DataProperty.EditType == BeanUtil.KT_COMBOBOXTREE_TYPE
                                || field.DataProperty.EditType == BeanUtil.KT_DATEFIELD_TYPE))
                        {
                            //添加显示字段
                            if (field.ViewBorder.ShInGrid)
                            {
                                BillQueryPrinterItem print = new BillQueryPrinterItem();
                                print.Key = field.Key;//setKey(field.getKey());
                                print.Caption = field.Caption;//setCaption(field.getCaption());
                                print.LanguageTexts = field.LanguageTexts;//setLanguageTexts(field.getLanguageTexts());
                                printFields.Fields.Add(print);
                            }
                            if (field.ViewBorder.ShSearch)
                            {
                                BillQueryPrinterItem gener = new BillQueryPrinterItem();
                                gener.Key = field.Key;//.setKey(field.getKey());
                                gener.Caption = field.Caption;//.setCaption(field.getCaption());
                                gener.LanguageTexts = field.LanguageTexts;//setLanguageTexts(field.getLanguageTexts());

                                //添加一般查询字段
                                if (field.ViewBorder.SearchType == BeanUtil.KT_QUERY_STNONE
                                        || field.ViewBorder.SearchType == BeanUtil.KT_QUERY_STGENERAL)
                                {
                                    generalFields.Fields.Add(gener);//addField(gener);
                                    
                                }
                                else if (field.ViewBorder.SearchType == BeanUtil.KT_QUERY_STADVANCED)
                                {
                                    //添加高级查询字段
                                    advancedFields.Fields.Add(gener);
                                }
                            }

                        }
                    }
                }
            }
        }


        /// <summary>
        /// 字段组多语言
        /// </summary>
        /// <param name="group"></param>
        /// <param name="default_"></param>
        private void createLanElement(BillFieldsI18nGroup group, bool default_)
        {
            //添加字段组及字段多语言
            if (FieldProperties != null && FieldProperties.Fields != null && FieldProperties.Fields.Count>0)
            {
                List<BillUiFieldsItem> item = FieldProperties.Fields;
                for (int k = 0; k < item.Count; k++)
                {
                    BillUiFieldsItem field = item[k];
                    if (!existLanguage(group, field.LanguageTexts, default_ ? field.Caption : ""))
                    {
                        BillFieldsI18nElement ele = new BillFieldsI18nElement();
                        ele.Key = field.LanguageTexts;
                        ele.Value = default_ ? field.Caption : "";
                        group.Element.Add(ele);
                    }
                }
            }

        }


        /// <summary>
        /// 判断多语言是否已经存在
        /// </summary>
        /// <param name="group"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private bool existLanguage(BillFieldsI18nGroup group, string key, string caption)
        {
            bool result = false;
            if (group != null && group.Element != null)
            {
                foreach (BillFieldsI18nElement i18 in group.Element)
                {
                    if (i18.Key == key)
                    {
                        if (caption != "")//存在时，如果是默认语言则修改为当前
                        {
                            i18.Value = caption;
                        }
                        result = true;
                        break;
                    }
                }
            }

            return result;
        }
    }
}
